package com.greenant.store.activity;

import java.text.DecimalFormat;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.greenant.store.R;
import com.greenant.store.bean.IncomeInfo;
import com.greenant.store.bean.UserIdentification;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.Const;
import com.lsarah.utils.SystemBarTintManager;

public class MyIncomeActivity extends BaseActivity {
	private TextView mTvMoneyKeTixian;
	private TextView mTvMoneyYiTixian;
	private TextView mTvAlpayRenzheng;

	private DecimalFormat mDecimalFormat;
	
	private IncomeInfo mIncomeInfo;
	private UserIdentification mUserIdentification;
	private Object mUserIdentificationError;
	private boolean isRetrieveUserIdentification = true;
	
	private final int RequestCodeWithdrawalsApply = 1;
	private final int RequestCodeBindAccount = 2;
	private final int RequestCodeChangeAccount = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.activity_myincome,
				getResources().getColor(R.color.firebrick));

		mDecimalFormat = new DecimalFormat("#########.##");

		mTvMoneyKeTixian = (TextView) findViewById(R.id.my_incomming_top_tixian_money);
		mTvMoneyYiTixian = (TextView) findViewById(R.id.my_incomming_yitixian_money);
		mTvAlpayRenzheng = (TextView) findViewById(R.id.my_incomming_my_alipay_prompt);

		this.retrieveIncomeInfo();
		this.retrieveUserIdentification();
	}
	
	private void retrieveIncomeInfo() {
		final AVUser current = AVUser.getCurrentUser();
		if (current != null) {
			String userId = current.getObjectId();
			// 获取收入信息
			RetrieveDataAsyn.getInstance().getIncomeInfo(userId, 0,
					new RetrieveDataListener() {
						@Override
						public void dataRetrieved(int requestId, Object data) {
							Log.d("CEXX", "CEXX-MyIncomeActivity-retrieveIncomeInfo-data:" + data);
							if (data != null) {
								mIncomeInfo = (IncomeInfo) data;
								String keTixianStr = mDecimalFormat.format(mIncomeInfo.getAccountBalance());
								String yiTixianStr = mDecimalFormat.format(mIncomeInfo.getWithdrawal());
								Log.d("CEXX", "CEXX-MyIncomeActivity-retrieveIncomeInfo-keTixianStr:" + keTixianStr);
								Log.d("CEXX", "CEXX-MyIncomeActivity-retrieveIncomeInfo-yiTixianStr:" + yiTixianStr);
								mTvMoneyKeTixian.setText(keTixianStr);
								mTvMoneyYiTixian.setText(yiTixianStr);
							}
						}
					});
		}
	}
	
	private void retrieveUserIdentification() {
		RetrieveDataAsyn.getInstance().getUserIdentification(0,
				new RetrieveDataListener() {
					@Override
					public void dataRetrieved(int requestId, Object data) {
						Log.d("CEXX", "CEXX-MyIncomeActivity-retrieveUserIdentification-data:" + data);
						isRetrieveUserIdentification = false;
						if (data != null) {
							if(data instanceof UserIdentification) {
								mUserIdentification = (UserIdentification) data;
							} else {
								mUserIdentificationError = data;
							}
						}

						refreshUserIdentification();
					}
				});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == RequestCodeWithdrawalsApply) {
			Const.mMyIncomeActivityIncomeInfo = null;
			if(resultCode == RESULT_OK) {
				if(mIncomeInfo != null) {
					String keTixianStr = mDecimalFormat.format(mIncomeInfo.getAccountBalance());
					String yiTixianStr = mDecimalFormat.format(mIncomeInfo.getWithdrawal());
					Log.d("CEXX", "CEXX-MyIncomeActivity-onActivityResult-keTixianStr:" + keTixianStr);
					Log.d("CEXX", "CEXX-MyIncomeActivity-onActivityResult-yiTixianStr:" + yiTixianStr);
					mTvMoneyKeTixian.setText(keTixianStr);
					mTvMoneyYiTixian.setText(yiTixianStr);
				}
			}
		} else if (requestCode == RequestCodeBindAccount) {
			if(resultCode == RESULT_OK) {
				mUserIdentification = Const.mUserIdentification;
				refreshUserIdentification();
				Const.mUserIdentification = null;
			}
		} else if (requestCode == RequestCodeChangeAccount) {
			if(resultCode == RESULT_OK) {
				refreshUserIdentification();
				Const.mUserIdentification = null;
			}
		}
	}
	
	private void refreshUserIdentification() {
		if(isRetrieveUserIdentification || mUserIdentificationError != null) {
			mTvAlpayRenzheng.setText("");
		} else {
			if (mUserIdentification != null && mUserIdentification.isAuthenticated()) {
				mTvAlpayRenzheng.setText(getResources().getString(R.string.my_incoming_my_yirenzheng));
			} else {
				mTvAlpayRenzheng.setText(getResources().getString(R.string.my_incoming_my_weirenzheng));
			}
		}
	}

	public void goBack(View view) {
		finish();
	}

	public void gotoKeTixian(View view) {
		Log.d("CEXX", "CEXX-MyIncomeActivity-gotoKeTixian");
		if(mIncomeInfo != null && mIncomeInfo.getAccountBalance() > 0) {
			Const.mMyIncomeActivityIncomeInfo = mIncomeInfo;
			Intent intent = new Intent(this, WithdrawalsApplyActivity.class);
			startActivityForResult(intent, RequestCodeWithdrawalsApply);
		}
	}

	public void gotoYiTixian(View view) {
		Log.d("CEXX", "CEXX-MyIncomeActivity-gotoYiTixian");
		if(mIncomeInfo != null) {
			Intent intent = new Intent(this, WithdrawListActivity.class);
			intent.putExtra("yitixian_money", mIncomeInfo.getWithdrawal());
			startActivity(intent);
		}
	}

	public void gotoDealling(View view) {
		Log.d("CEXX", "CEXX-MyIncomeActivity-gotoDealling");
	}

	public void gotoMyAlipay(View view) {
		Log.d("CEXX", "CEXX-MyIncomeActivity-gotoMyAlipay");
		if(isRetrieveUserIdentification || mUserIdentificationError != null) {
			
		} else {
			if(mUserIdentification != null) {
				Const.mUserIdentification = mUserIdentification;
				Intent intent = new Intent(this, MyAlipayActivity.class);
				startActivityForResult(intent, RequestCodeChangeAccount);
			} else {
				Intent intent = new Intent(this, MyAlipayBindAccountActivity.class);
				startActivityForResult(intent, RequestCodeBindAccount);
			}
		}
	}

	public void gotoEarningStream(View view) {
		Intent intent = new Intent(this, MyEarningActivity.class);
		startActivity(intent);
	}
}
